package cn.it.shop.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import javax.annotation.Resource;
import javax.servlet.ServletContext;

import org.springframework.stereotype.Component;

import cn.it.shop.pojo.Category;
import cn.it.shop.pojo.Goods;
import cn.it.shop.service.CategoryService;
import cn.it.shop.service.GoodsService;

/*
 * 此类是一个线程, 要实现加载首页商品信息的功能
 * */
@Component(value="shopTimerTask")
public class ShopTimerTask extends TimerTask {

	@Resource
	private CategoryService categoryService = null;

	@Resource
	private GoodsService goodsService = null;
	// 通过监听器传入,而非Spring注入
	private ServletContext application=null;

	public void setApplication(ServletContext application) {
		this.application = application;
	}

	@Override
	public void run() {
		System.out.println("-----run()------");
		// TODO Auto-generated method stub
		List<Category> categoryList = categoryService.queryByHot(true);
		// 查询每一个热点类别的推荐商品,查询出来之后要存放到大集合中
		List<List<Goods>> bigList = new ArrayList<List<Goods>>();
		for (Category category : categoryList) {
			List<Goods> goodsList = goodsService.queryByCommendAndOpen(category
					.getId());
			bigList.add(goodsList);
		}
		// 把大集合存储到application内置对象中
		application.setAttribute("bigList", bigList);
	}

}
